package com.paradox.aggregation.provider.proxy;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class CgLibProxy implements DynamicProxy, MethodInterceptor {

    private Class targetClass;

    public CgLibProxy(Class targetClass){
        this.targetClass=targetClass;
    }

    @Override
    public Object createProxy() {
        Enhancer enhancer=new Enhancer();
        enhancer.setSuperclass(targetClass);
        enhancer.setCallback(this);
        return enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("Before Method Invoke...");
        methodProxy.invokeSuper(o, objects);
        System.out.println("After Method Invoke...");
        return null;
    }
}
